package com.songtech.userstatus.controller;


import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.stateuser.bean.UserStatus;
import com.songtech.stateuser.service.UserStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: we
 * Date: 2018-06-21 15:35
 */
@RestController
@RequestMapping("/user")
public class UserStatusController {

    Logger logger = LoggerFactory.getLogger(UserStatusController.class);

    @Autowired
    private UserStatusService userStatusService;

    /**
     * 查询用户在线和离线数
     * @param map
     * @return
     */
    @RequestMapping(value="/userStatus",method = RequestMethod.GET)
    public JsonResult userStatus(@RequestParam Map<String,Object> map){
        Map<String,Object> hashMap = null;
        try{
            hashMap = new HashMap<>();
            int offlineCount = userStatusService.offlineUserCount(map);
            int onlineCount = userStatusService.onlineUserCount(map);
            hashMap.put("onlineCount",onlineCount);
            hashMap.put("offlineCount",offlineCount);
        }catch (Exception e){
            logger.error("查询用户在线和离线数异常："+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 查询用户总数和用户新增数
     * @param map
     * @return
     */
    @RequestMapping(value="/getUserOnline",method = RequestMethod.GET)
    public JsonResult getUserOnline(@RequestParam Map<String,Object> map){
        Map<String,Object> hashMap = null;
        try{
            hashMap = new HashMap<>();
            List<UserStatus> userOnlineCount = userStatusService.onlineUserByDay(map);
            hashMap.put("userCountList",userOnlineCount);
        }catch (Exception e){
            logger.error("统计用户总数和用户新增数异常："+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }
}
